﻿using System;
using System.Collections.Generic;
using System.Threading;
namespace Host
{
    public class HostManager
    {
        private List<Host> host_list = new List<Host>();
        private List<Host> new_host_list = new List<Host>();
        private static HostManager host_manager = new HostManager();
        private bool is_runing = false;
        private Thread thread = null;

        private HostManager() { }

        /// <summary>
        /// 获取对应单例
        /// </summary>
        /// <returns>The instance.</returns>
        public static HostManager GetInstance()
        {
            return host_manager;
        }

        public void Start()
        {
            is_runing = true;
            if (thread == null)
            {
                thread = new Thread(() =>
                {
                    while (is_runing || GetHostCount() > 0)
                    {
                        Update();
                        Thread.Sleep(host_manager.GetHostCount() > 0 ? 100 : 1000);
                    }

                    thread = null;
                });
                thread.Start();
            }
        }

        public void Stop()
        {
            is_runing = false;
            ClearAll();
        }

        public IEnumerable<Host> GetAll()
        {
            List<Host> hosts = new List<Host>();

            hosts.AddRange(new_host_list);
            hosts.AddRange(host_list);

            return hosts;
        }

        public void ClearAll()
        {
            foreach (var item in host_list)
            {
                item.DestroyMe = true;
            }

            foreach (var item in new_host_list)
            {
                item.DestroyMe = true;
            }
        }

        /// <summary>
        /// 添加托管内容
        /// </summary>
        /// <param name="host">Host.</param>
        public void AddHost(Host host)
        {
            if (host != null)
            {
                host.OnEnter();
                host.DestroyMe = false;
                new_host_list.Add(host);
            }
        }

        public void AddHostRange(IEnumerable<Host> host)
        {
            foreach (var item in host ?? new List<Host>())
            {
                AddHost(item);
            }
        }

        /// <summary>
        /// 更新业务逻辑
        /// </summary>
        public void Update()
        {
            host_list.AddRange(new_host_list);
            new_host_list.Clear();

            foreach (var item in host_list)
            {
                try
                {
                    item.Update();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            for (int i = host_list.Count - 1; i >= 0; --i)
            {
                var item = host_list[i];
                if (item.DestroyMe)
                {
                    item.OnExit();
                    host_list.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 获取托管数量
        /// </summary>
        /// <returns>The host count.</returns>
        public int GetHostCount()
        {
            return host_list.Count + new_host_list.Count;
        }
    }
}
